package com.xry.common.util;

import java.util.Random;

public class VerificationCodeUtil {
    // 验证码有效期5分钟
    private static final long EXPIRY_TIME = 1000 * 60 * 5;
    private static final ExpiryMap expiryMap = new ExpiryMap();

    // 生成6位数字验证码
    public static String generateCode() {
        Random random = new Random();
        StringBuilder code = new StringBuilder();
        for (int i = 0; i < 6; i++) {
            code.append(random.nextInt(10));
        }
        return code.toString();
    }

    // 存储验证码
    public static void storeCode(String email, String code) {
        expiryMap.put(email, code, EXPIRY_TIME);
    }

    // 验证验证码
    public static boolean verifyCode(String email, String code) {
        String storedCode = (String) expiryMap.get(email);
        return code != null && code.equals(storedCode);
    }

    // 移除验证码
    public static void removeCode(String email) {
        expiryMap.remove(email);
    }
}